package com.myapp.mobilesafe.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.mobilesafe.utils.MD5Utils;
import com.myapp.mobilesafe.utils.PrefUtils;

public class HomeActivity extends AppCompatActivity {
    private GridView gvHome;
    private String[] funcNames = new String[]{"手机防盗","通讯卫士","软件管理","进程管理",
            "流量统计","手机杀毒","缓存清理","高级工具","设置中心"};
    private int[] funcImgs =new int[]{R.drawable.muma,R.drawable.weishi,R.drawable.ruanjian,
    R.drawable.jinchengguanli,R.drawable.liuliang,R.drawable.shadu,R.drawable.qingli,R.drawable.gaoji
    ,R.drawable.shezhi};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        gvHome = (GridView) findViewById(R.id.gv_home);
        gvHome.setAdapter(new MyGVAdapter());
        gvHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        showSafeDialog();
                        break;
                    case 1:
                        startActivity(new Intent(getApplicationContext(), BlackListActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(getApplicationContext(),AppManagerActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(getApplicationContext(),ProcessManagerActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(getApplicationContext(),TrafficStatsActivity.class));
                        break;
                    case 5:
                        startActivity(new Intent(getApplicationContext(),AntiVirusActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(getApplicationContext(),CleanCacheActivity.class));
                        break;
                    case 7:
                        startActivity(new Intent(HomeActivity.this,AdvanceToolsActivity.class));
                        break;
                    case 8:
                        Intent intent = new Intent(HomeActivity.this,SettingActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }
    private void showSafeDialog() {
        String pwd = PrefUtils.getString("password",null,this);
        if(!TextUtils.isEmpty(pwd)){
            showInputPwdDialog();
        }else {
            showSetPwsDialog();
        }
    }

    private void showInputPwdDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this,R.layout.dialog_input_pwd,null);
        dialog.setView(view, 0, 0, 0, 0);
        Button btnSure = (Button) view.findViewById(R.id.btn_ensure);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        final EditText etPwd = (EditText) view.findViewById(R.id.et_pwd);
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = etPwd.getText().toString().trim();
                if(!TextUtils.isEmpty(pwd)){
                    String savePwd = PrefUtils.getString("password",null,getApplicationContext());
                    if(MD5Utils.md5(pwd).equals(savePwd)){
                        dialog.dismiss();
                        startActivity(new Intent(getApplicationContext(),SafeLocationActivity.class));
                    }else {
                        Toast.makeText(getApplicationContext(),"密码不正确！！",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(getApplicationContext(),"密码不能为空!!!",Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void showSetPwsDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this,R.layout.dialog_set_pwd,null);
        dialog.setView(view, 0, 0, 0, 0);
        Button btnSure = (Button) view.findViewById(R.id.btn_ensure);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        final EditText etPwd = (EditText) view.findViewById(R.id.et_pwd);
        final EditText etPwdEnsure = (EditText) view.findViewById(R.id.et_pwd_ensure);
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = etPwd.getText().toString().trim();
                String pwdEnsure = etPwdEnsure.getText().toString().trim();
                if(!TextUtils.isEmpty(pwd) &&!TextUtils.isEmpty(pwdEnsure)){
                    if(pwd.equals(pwdEnsure)){
                        PrefUtils.putString("password",MD5Utils.md5(pwd),getApplicationContext());
                        dialog.dismiss();
                        startActivity(new Intent(getApplicationContext(), SafeLocationActivity.class));
                    }else {
                        Toast.makeText(getApplicationContext(),"密码不一致!!!",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(getApplicationContext(),"密码不能为空!!!",Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    class MyGVAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return funcNames.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(HomeActivity.this,R.layout.grid_item_home,null);
            TextView tvName = (TextView) view.findViewById(R.id.tv_item);
            ImageView ivName = (ImageView) view.findViewById(R.id.iv_item);
            tvName.setText(funcNames[position]);
            ivName.setImageResource(funcImgs[position]);
            return view;
        }
    }
}
